package hadoop.mapreduce.wordcountcombiner;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 自定义map阶段合并,减轻reducer的压力.注意根据需求合并,莫要画蛇添足
 *
 * 这里的输入拿到的是Mapper的输出
 * 输入模板{a,1} {a,1} {b,1} {c,1} {c,1}
 * 这里的输出是Reducer的输入
 */
public class WordcountCombiner extends Reducer<Text, IntWritable,Text, IntWritable>  {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum=0;
        //map阶段合并 {a,1} {a,1} {b,1} {c,1} {c,1}  -->{a,2} {b,1} {c,2}
        for (IntWritable count : values) {
            sum+=count.get();
        }
        context.write(key,new IntWritable(sum));
    }

}
